package com.burgeon.backend.SHOP.rest;
/**
 * 支付 可使用优惠券
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class UseCoupon extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public UseCoupon(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Coupon> getData(String phone,String price) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone,price), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Integer dateString = Integer.parseInt(sdf.format(now));

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Coupon> data = new ArrayList<Coupon>();
        for (int i = 0; i < jsonArray.length(); i++) {
            Coupon coupon = new Coupon();
            coupon.setId(jsonArray.getJSONArray(i).getString(0));
            coupon.setCode(jsonArray.getJSONArray(i).getString(1));
            coupon.setType(jsonArray.getJSONArray(i).getString(2));
            coupon.setCheckno(jsonArray.getJSONArray(i).getString(3));
            coupon.setMoney(jsonArray.getJSONArray(i).getString(4));
            coupon.setDatebeg(jsonArray.getJSONArray(i).getString(5));
            coupon.setDateend(jsonArray.getJSONArray(i).getString(6));
            coupon.setVerifyed(jsonArray.getJSONArray(i).getString(7));
            coupon.setValided(jsonArray.getJSONArray(i).getString(8));
            coupon.setOriginal(jsonArray.getJSONArray(i).getString(9));
            coupon.setDiscount(jsonArray.getJSONArray(i).getString(10));
            /*if (dateString > Integer.parseInt(coupon.getDateend())) {
                coupon.setValided("否");
            }
            data.add(coupon);*/
            if (dateString <= Integer.parseInt(coupon.getDateend())
                    && dateString >= Integer.parseInt(coupon.getDatebeg())
                    &&"否".equals(coupon.getVerifyed())
                    &&"是".equals(coupon.getValided())
            ){
                //coupon.setValided("否");
                data.add(coupon);
            }
        }
        return data;
    }

    private MultiValueMap Data(String phone,String price) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("VOUCHERS_NO");//券号
        columns.add("VOU_TYPE");//类型
        columns.add("IDENTIFY_CODE");//检验码
        columns.add("AMT_DISCOUNT");//面值
        columns.add("START_DATE");//开始时间
        columns.add("VALID_DATE");//结束时间
        columns.add("IS_VERIFYED");//核销标记
        columns.add("IS_VALID");//有效标记
        columns.add("AMT_NOLES");//原价不小于
        columns.add("VOU_DIS");//折扣

        params.put("columns", columns);
        params.put("table", "C_V_VOUCHERS");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");
        expr1v1.put("column", "U_SCENARIOS");
        expr1v1.put("condition", "'<3'");
        expr1.put("expr1", expr1v1);
        expr1v2.put("column", "'AMT_NOLES'");
        expr1v2.put("condition", "'<"+(Integer.parseInt(price)+1)+"'");
        expr1.put("expr2", expr1v2);

        String ac = "'" + phone + "'";
        paramss.put("condition", ac);//查询条件
        expr2.put("column", "'C_VIP_ID;CARDNO'");
        expr2.put("condition", ac);

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        UseCoupon g = new UseCoupon("39.99.239.133:55321","nea@burgeon.com.cn","burgeon55321");
        try {
            System.err.println(g.getData("17732141036","1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
